package com.spx.kernel.datasource.preferences;

import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;

import com.spx.kernel.KernelActivator;

/**
 * This class represents a preference page that is contributed to the
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
 * can use the field support built into JFace that allows us to create a page
 * that is small and knows how to save, restore and apply itself.
 * <p>
 * This page is used to modify preferences only. They are stored in the
 * preference store that belongs to the main plug-in class. That way,
 * preferences can be accessed directly via the preference store.
 */

public class DataSourcePreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
{

	public DataSourcePreferencePage()
	{
		super(GRID);
		
		
	
        
  
       setPreferenceStore(KernelActivator.getDefault().getPreferenceStore());
       loadState();
		
	
		
		setDescription("A demonstration of a preference page implementation");
	}

	private IEclipsePreferences getEclipsePreferences() {
		IEclipsePreferences preferences = new  ConfigurationScope().getNode( KernelActivator.PLUGIN_ID+"/"+PreferenceConstants.P_DATASOURCE_NODE_NAME);
		return preferences;
	}

	/**
	 * Creates the field editors. Field editors are abstractions of the common
	 * GUI blocks needed to manipulate various types of preferences. Each field
	 * editor knows how to save and restore itself.
	 */
	@Override
	public void createFieldEditors()
	{

		addField(new StringFieldEditor(PreferenceConstants.P_DATASOURCE_DRIVER, "&Data Source Driver:", getFieldEditorParent()));
		addField(new StringFieldEditor(PreferenceConstants.P_DATASOURCE_URL, "Data Source &URL", getFieldEditorParent()));
		addField(new StringFieldEditor(PreferenceConstants.P_DATASOURCE_USER, "Data Source User &Name", getFieldEditorParent()));
		addField(new StringFieldEditor(PreferenceConstants.P_DATASOURCE_PASSWORD, "Data Source &Password", getFieldEditorParent()));
		addField(new BooleanFieldEditor(PreferenceConstants.P_DATASOURCE_REBUILD, "Data Source &Build", getFieldEditorParent()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench)
	{

	}

	@Override
	protected void performApply()
	{
		KernelActivator.getDefault();
		super.performApply();
		try {
			saveState();
		} catch (BackingStoreException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean isValid()
	{
		final boolean result = super.isValid();
		return result;
	}

	@Override
	public boolean performOk() {
		
		boolean retVal =super.performOk();
		try {
			if (retVal == true){
				saveState();
			}
		} catch (BackingStoreException e) {
		    retVal = false;
		
		}
		return retVal;
	}

	
	
	
	private void   saveState() throws BackingStoreException
	{
        IEclipsePreferences preferences = getEclipsePreferences();
        preferences.put(PreferenceConstants.P_DATASOURCE_DRIVER  , getPreferenceStore().getString(PreferenceConstants.P_DATASOURCE_DRIVER));
        preferences.put(PreferenceConstants.P_DATASOURCE_URL     , getPreferenceStore().getString(PreferenceConstants.P_DATASOURCE_URL));
        preferences.put(PreferenceConstants.P_DATASOURCE_USER    , getPreferenceStore().getString(PreferenceConstants.P_DATASOURCE_USER));
        preferences.put(PreferenceConstants.P_DATASOURCE_PASSWORD, getPreferenceStore().getString(PreferenceConstants.P_DATASOURCE_PASSWORD));
        preferences.put(PreferenceConstants.P_DATASOURCE_REBUILD , getPreferenceStore().getString(PreferenceConstants.P_DATASOURCE_REBUILD));
        preferences.flush();
        preferences.sync();
		
	}
	
	private void   loadState(){
		

        
        IEclipsePreferences preferences = getEclipsePreferences();


        getPreferenceStore().putValue(PreferenceConstants.P_DATASOURCE_DRIVER, preferences.get(PreferenceConstants.P_DATASOURCE_DRIVER, "Data Source Driver:"));
        getPreferenceStore().putValue(PreferenceConstants.P_DATASOURCE_URL,preferences.get(PreferenceConstants.P_DATASOURCE_URL, "Data Source URL"));
        getPreferenceStore().putValue(PreferenceConstants.P_DATASOURCE_USER,preferences.get(PreferenceConstants.P_DATASOURCE_USER, "Data Source User Name" ));
        getPreferenceStore().putValue(PreferenceConstants.P_DATASOURCE_PASSWORD,preferences.get(PreferenceConstants.P_DATASOURCE_PASSWORD, "Data Source Password"));
        getPreferenceStore().putValue(PreferenceConstants.P_DATASOURCE_REBUILD,preferences.get(PreferenceConstants.P_DATASOURCE_REBUILD, "Data Source Build"));

       
	}
	
	
	
}